package hk.linkcredit.walletservice.wallet.service.strategy;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import hk.linkcredit.walletservice.constant.Constants;
import hk.linkcredit.walletservice.constant.Status;
import hk.linkcredit.walletservice.constant.WalletConstants;
import hk.linkcredit.walletservice.exception.BizException;
import hk.linkcredit.walletservice.utils.StringUtils;
import hk.linkcredit.walletservice.wallet.domain.entity.WalletAmount;
import hk.linkcredit.walletservice.wallet.domain.entity.WalletOrder;
import hk.linkcredit.walletservice.wallet.service.WalletAmountService;
import hk.linkcredit.walletservice.wallet.service.WalletOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author: jamnsin
 * @Date: 2022/6/12 16:33
 */
@Slf4j
@Component
public class OrderStatusService {

    @Autowired
    WalletOrderService walletOrderService;

    @Autowired
    WalletAmountService walletAmountService;

    protected WalletOrder getWalletOrder(String custNo, String serialNo) {
        QueryWrapper<WalletOrder> queryWrapper = new QueryWrapper<WalletOrder>()
                .eq("serial_no", serialNo)
                // .eq("cust_no", custNo)
                .eq("state", Constants.BIZ_STATUS_1)
                .select("id", "cust_no", "wallet_no", "serial_no", "wallet_type", "order_type", "amount", "service_fee", "order_status", "from_where", "to_dest", "loan_serial_no")
                .last("order by create_time desc limit 1");

        WalletOrder walletOrder = walletOrderService.getOne(queryWrapper);
        if (walletOrder == null) {
            throw new BizException(Status.WALLET_ORDER_IS_NOT_EXISTS);
        }

        log.debug("当前订单信息：{}", walletOrder);
        return walletOrder;
    }

    protected List<WalletOrder> getWalletOrders(String custNo, String serialNo) {
        QueryWrapper<WalletOrder> queryWrapper = new QueryWrapper<WalletOrder>()
                .eq("batch_serial_no", serialNo)
                .eq("state", Constants.BIZ_STATUS_1)
                .select("id", "cust_no", "wallet_no", "serial_no", "wallet_type", "order_type", "amount", "service_fee", "order_status", "from_where", "to_dest", "loan_serial_no")
                .last("order by create_time desc");

        List<WalletOrder> walletOrders = walletOrderService.list(queryWrapper);
        if (CollectionUtils.isEmpty(walletOrders)) {
            throw new BizException(Status.WALLET_ORDER_IS_NOT_EXISTS);
        }

        log.debug("当前订单信息：{}", walletOrders);
        return walletOrders;
    }

    protected void updateWalletOrder(Long orderId, String orderStatus, String message) {
        UpdateWrapper<WalletOrder> wrapper = Wrappers.update();
        wrapper.set("order_status", orderStatus)
                .set("remark", message)
                .eq("id", orderId);
        walletOrderService.update(wrapper);
    }

    protected void unfreezeWalletAmount(WalletOrder walletOrder) {
        WalletAmount walletAmount = WalletAmount.builder()
                .custNo(walletOrder.getCustNo())
                .walletType(walletOrder.getWalletType())
                .frozenAmount(walletOrder.getAmount().add(walletOrder.getServiceFee()))
                .build();
        walletAmountService.unfreezeWalletAmount(walletAmount);
    }

    protected boolean isRepayOrder(String orderType) {
        return StringUtils.equalsAny(orderType, WalletConstants.ORDER_TYPE_REPAY, WalletConstants.ORDER_TYPE_DEFERRED_REPAY);
    }

    protected boolean checkCustNo(String desCustNo, String tarCustNo) {
        return StringUtils.isNotBlank(desCustNo) && !StringUtils.equals(desCustNo, tarCustNo);
    }

    protected void updateWalletOrder(WalletOrder walletOrder) {
        UpdateWrapper<WalletOrder> wrapper = Wrappers.update();
        wrapper.set("order_status", walletOrder.getOrderStatus())
                .set("remark", walletOrder.getRemark())
                .set("update_by", walletOrder.getUpdateBy())
                .eq("id", walletOrder.getId());
        walletOrderService.update(wrapper);
    }
}
